package com.xzr.hospitalregister.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xzr.hospitalregister.R;
import com.xzr.hospitalregister.activity.BBGXActivity;
import com.xzr.hospitalregister.config.HttpUrl;
import com.xzr.hospitalregister.entity.SomethingNeed;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Administrator on 2018/4/14 0014.
 */

public class DialogIpSet extends DialogFragment {
    private TextView nowIp;
    private EditText newIp, newDuanKou;
//    private TextView newSelect;
    private Button certain;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.dialog_ip_set,null);
        initWidget(inflate);
        initData();
        return inflate;
    }

    private void initWidget(View inflate){
        nowIp = (TextView) inflate.findViewById(R.id.tv_acitivity_ip_set_nowIp);
        newIp = (EditText) inflate.findViewById(R.id.tv_acitivity_ip_set_newIp);
        newDuanKou = (EditText) inflate.findViewById(R.id.tv_acitivity_ip_set_newDuanKou);
        certain = (Button) inflate.findViewById(R.id.btn_activity_ip_set_certainNew);
//        newSelect = (TextView) inflate.findViewById(R.id.tv_acitivity_ip_set_newSelect);
    }
    private void initData(){
        SharedPreferences sp = getActivity().getSharedPreferences("IpSet", MODE_PRIVATE);
        String ipValue = sp.getString("ipValue", null);
        if (ipValue == null) {
            nowIp.setText("当前IP为空！");
            nowIp.setTextColor(getResources().getColor(R.color.red));
        } else {
            nowIp.setText("当前IP：" + ipValue);
            nowIp.setTextColor(getResources().getColor(R.color.black));
        }

//        newSelect.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DialogIpSelect dialogIpSelect = new DialogIpSelect();
//                dialogIpSelect.show(getSupportFragmentManager(), "DialogIpSelect");
//            }
//        });

        certain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(newIp.getText().toString()) && !TextUtils.isEmpty(newDuanKou.getText().toString())) {

                    SharedPreferences sp =getActivity().getSharedPreferences("IpSet", MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    String value = newIp.getText().toString() + ":" + newDuanKou.getText().toString();
                    edit.putString("ipValue", value);//设置默认ip
                    edit.commit();
                    nowIp.setText("当前IP：" + value);
                    SomethingNeed.getInstance().setIp(value);
                    HttpUrl.initUrl();
                    Log.e("TAG","ipvalue++++++++++++++++++++++"+value);
                    Toast.makeText(getActivity(), "设置成功", Toast.LENGTH_SHORT).show();
                    dismiss();
                    startActivity(new Intent(getActivity(), BBGXActivity.class));
                    System.exit(0);
                } else {
                    Toast.makeText(getActivity(), "请填写IP地址和端口号！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
//    public void setSelectIpToWidget(DialogIpSelectItem dialogIpSelectItem) {
//        newIp.setText(dialogIpSelectItem.getCityIp().split(":")[0]);
//        newDuanKou.setText(dialogIpSelectItem.getCityIp().split(":")[1]);
//    }
}
